package cn.woblog.recyclerviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.helper.ItemTouchHelper;
import cn.woblog.recyclerviewsample.adapter.SwipeRecyclerViewMenuAdapter;
import java.util.ArrayList;
import java.util.List;

public class SwipeMenuActivity extends AppCompatActivity {

  private SwipeRecyclerViewMenuAdapter adapter;
  private List<String> data;
  private ItemTouchHelper itemTouchHelper;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipe_menu);

    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    adapter = new SwipeRecyclerViewMenuAdapter(this);
    rv.setAdapter(adapter);

    data = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
      data.add("Item " + i);
    }

    adapter.setData(data);

//    itemTouchHelper = new ItemTouchHelper(new MyCallback());
//    itemTouchHelper.attachToRecyclerView(rv);
  }

  class MyCallback extends ItemTouchHelper.Callback {

    @Override
    public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) {
      return makeMovementFlags(0, ItemTouchHelper.LEFT);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) {

      return false;
    }

    @Override
    public void onSwiped(ViewHolder viewHolder, int direction) {
      int position = viewHolder.getAdapterPosition();
      adapter.removeData(position);
    }

//    @Override
//    public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) {
//      //这样要重置之前设置的参数
//      viewHolder.itemView.setAlpha(1);
//      viewHolder.itemView.setScaleX(1);
//      viewHolder.itemView.setScaleY(1);
//      super.clearView(recyclerView, viewHolder);
//    }
//
//    @Override
//    public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX,
//        float dY, int actionState, boolean isCurrentlyActive) {
//
//      if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
//        float progress = (float) (1 - Math.abs(dX) * 1.0 / viewHolder.itemView.getRootView()
//            .getWidth());
//        viewHolder.itemView.setScaleX(progress);
//        viewHolder.itemView.setScaleY(progress);
//        viewHolder.itemView.setAlpha(progress);
//      }
//      super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
//    }
  }
}
